//登录注册tab
function setTab(name, cursel, n) {
    for (i = 1; i <= n; i++) {
        var menu = document.getElementById(name + i);
        var con = document.getElementById("con_" + name + "_" + i);
        menu.className = i == cursel ? "cur" : "";
        con.style.display = i == cursel ? "block" : "none";
    }
}

function showErrorMsg(msg) {
    layer.msg(msg, {icon: 5, time: 2000});
}

function showErrorAlert(msg, icon) {
    if (!icon && icon != 0) {
        icon = 5;
    }
    layer.alert(msg, {icon: icon, title: false, closeBtn: false});
}

// 加载层
function layer_loading(msg) {
    var loading = layer.msg(
        msg + '...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请勿刷新页面',
        {
            icon: 1,
            time: 3600000, //1小时后后自动关闭
            shade: [0.2] //0.1透明度的白色背景
        });
    //loading层
    var index = layer.load(3, {
        shade: [0.1, '#fff'] //0.1透明度的白色背景
    });

    return loading;
}

//收藏
function saveCollection(that) {
    var artId = $(that).attr("dataId");
    $.ajax({
        url: "/user/updateCollection.html",
        data: {
            "articleId": artId
        },
        type: 'post',
        success: function (res) {
            //
            if (res.rt == -1) {
                let reqUrl=location.href.split("http://")[1] || location.href.split("https://")[1];
                let redirectUrl=reqUrl.substring(reqUrl.indexOf("/")+1);
                if(redirectUrl==null || redirectUrl=='' || typeof redirectUrl === "undefined"){
                    redirectUrl="/";
                }
                layer.msg("未登录,请登录！", {time: 1000}, function () {
                    window.location.href = "/to/login.html?redirectUrl=" +redirectUrl
                });
            } else if (res.rt == -2) {
                showErrorMsg("收藏失败!");
                return;
            } else if (res.rt == 1) {
                layer.msg('取消收藏!', {icon: 1, time: 2000});
                $(that).removeClass("ok");
                return;
            } else if (res.rt == 2) {
                layer.msg('收藏成功!', {icon: 1, time: 2000});
                $(that).addClass("ok");
                return;
            } else {
                showErrorMsg("网络异常,请稍后再试!");
                return;
            }
        },
        error: function (e) {
            showErrorAlert("网络异常,请稍后再试!");
        }
    });

}


// 上传头像
function upload_head_pic(e) {
    var file = $(e)[0].files[0];
    if (!file) {
        return false;
    }
    var Max_Size = 2000; //2M
    if (file.size > Max_Size * 1024) {
        showErrorAlert("请上传小于或等于2M的图片！");
        return false;
    }
    layer_loading('正在处理');
    var formData = new FormData();
    formData.append('file', file);
    $.ajax({
        type: "post",
        url: "/user/uploadImage",
        data: formData,
        processData: false,
        contentType: false,
        success: function (res2) {
            layer.closeAll();
            if (res2.rt!=null) {
                if (-1 == res2.rt) {
                    showErrorAlert("未登录,请登录！");
                }else if (-2 == res2.rt) {
                    showErrorAlert(res2.message);
                }else if (-3 == res2.rt) {
                    showErrorAlert(res2.message);
                }else {
                    let userImage=res2.rt;
                    $.ajax({
                        url: "/user/updateUserImage.html",
                        data: {
                            "userImage": userImage
                        },
                        type: 'post',
                        success: function (res) {
                            layer.closeAll();
                            if (1 == res.rt) {
                                $('#ey_head_pic_a').attr('src', userImage);
                                layer.msg("保存成功");
                            } else if (-1 == res.rt) {
                                showErrorAlert("未登录,请登录！");
                            } else {
                                showErrorMsg("网络异常,请稍后再试!");
                            }
                        },
                        error: function (e) {
                            layer.closeAll();
                            showErrorAlert("网络异常,请稍后再试!");
                        }
                    });
                }
            } else {
                layer.closeAll();
                showErrorAlert("网络异常,请稍后再试！");
            }
        },
        error: function (e) {
            layer.closeAll();
            showErrorAlert(e.message);
        }
    });
}


// 签到
function userSignin() {
    layer_loading('正在处理');
    $.ajax({
        type: "get",
        url: "/user/sign.html",
        success: function (res) {
            layer.closeAll();
            if (res.rt == 1) {
                layer.msg("签到成功", {icon: 6, time: 1500});
                location.reload();
            } else if (-1 == res.rt) {
                showErrorAlert("未登录,请登录！");
            } else if (res.rt == 2) {
                layer.msg("已签到,请勿重复签到", {icon: 6, time: 1500});
                location.reload();
            } else {
                showErrorAlert("网络异常,请稍后再试!");
            }
        },
        error: function (e) {
            layer.closeAll();
            showErrorAlert("网络异常,请稍后再试!");
        }
    });
}

//兑换
function goToMoney() {
    window.location.href="/person/userpayrecharge.html";
}

/**
 * 发送消息
 */
function sendUserMessage(){


}


//更新资料
// 修改会员属性信息
function UpdateUsersData() {
    layer_loading('正在处理');
    var password = $("#password_edit").val()
    if (!password) {
        layer.msg("操作成功", {time: 2000}, function () {
            location.reload();
        });
    } else {
        $.ajax({
            url: "/user/editUserDetail.html",
            data: {
                "password": password
            },
            type: 'post',
            success: function (res) {
                layer.closeAll();
                if (1 == res.rt) {
                    layer.msg("操作成功", {time: 1000}, function () {
                        location.reload();
                    });
                } else if (-1 == res.rt) {
                    showErrorAlert("未登录,请登录！");
                } else {
                    showErrorMsg("网络异常,请稍后再试!");
                }
            },
            error: function (e) {
                layer.closeAll();
                showErrorAlert("网络异常,请稍后再试!");
            }
        });
    }
};


//一键分享
function shareTo(types, textStr = '') {
    var title, imageUrl, url, description, keywords;
    //获取文章标题
    title = document.title;
    //获取网页中内容的第一张图片地址作为分享图
    imageUrl = $("#articleImgId").attr("src");
    //当内容中没有图片时，设置分享图片为网站logo
    if (typeof imageUrl == 'undefined') {
        imageUrl = 'https://' + window.location.host + '/static/images/logo.png';
    }
    //获取当前网页url
    url = document.location.href;
    //获取网页描述
    description = document.querySelector('meta[name="description"]').getAttribute('content');
    //获取网页关键字
    keywords = document.querySelector('meta[name="keywords"]').getAttribute('content');
    //qq空间接口的传参
    if (types == 'qzone') {
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&sharesource=qzone&title=' + title + '&pics=' + imageUrl + '&summary=' + description);
    }
    //新浪微博接口的传参
    if (types == 'sina') {
        window.open('http://service.weibo.com/share/share.php?url=' + url + '&sharesource=weibo&title=' + title + '&pic=' + imageUrl + '&appkey=2706825840');
    }
    //qq好友接口的传参
    if (types == 'qq') {
        window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + url + '&sharesource=qzone&title=' + title + '&pics=' + imageUrl + '&summary=' + description + '&desc=' + keywords);
    }
}

